var bookModel = require('../models/books');
var colmunModel=require('../models/colmun');
var authorModel=require('../models/author');
const classify = {
    classify: (req, res, next) => {
        if (req.query.colmun_id) {
            colmunModel.find({}).then(doc1=>{
                bookModel.find({ colmun_id: req.query.colmun_id }).populate('colmun_id').populate('author_id').then(doc => {
                    res.render('classify', { book: doc, colmun:doc1,colmun_id: req.query.colmun_id, loginUser: req.session.loginUser })
                }).catch(err => {
                    console.log(err)
                })
            })
        } else {
            res.render('classify', { classify: null, loginUser: req.session.loginUser })
        }
    },

    //获取栏目书籍
    book_list: (req, res, next) => {
        bookModel.find({ colmun_id: req.body.id }).populate('colmun_id').populate('author_id').then(doc => {
            authorModel.find({ colmun: req.body.id }).populate('colmun_id').then(doc2=>{
                res.json({
                    status: 1,
                    result: doc,
                    author:doc2
                })
            })
        }).catch(err => {
            console.log(err)
        })
    },
    //获取栏目下作者书籍
    author_list_book: (req, res, next) => {
        bookModel.find({ author_id: req.body.id }).populate('colmun_id').populate('author_id').then(doc => {
            res.json({
                status: 1,
                result: doc
            })
        })
    }
}
module.exports = classify;